package Array;

import org.junit.Test;

import java.util.HashMap;

/**
 * @BelongsProject: SeniorArchitect-LeetCode
 * @BelongsPackage: Array
 * @Author: zhuangxiaoyan
 * @CreateTime: 2023-10-21  08:22
 * @Description: TODO
 * @Version: 1.0
 */
public class 罗马数字转整数13 {

    public int romanToInt(String s) {
        HashMap<Character, Integer> hashMap = new HashMap<>();
        hashMap.put('I', 1);
        hashMap.put('V', 5);
        hashMap.put('X', 10);
        hashMap.put('L', 50);
        hashMap.put('C', 100);
        hashMap.put('D', 500);
        hashMap.put('M', 1000);
        int result = 0;
        for (int i = 0; i < s.length(); i++) {
            if (i == s.length() - 1) {
                result += hashMap.get(s.charAt(i));
                continue;
            }
            if ((s.charAt(i) == 'I' && (s.charAt(i + 1) == 'V' || s.charAt(i + 1) == 'X'))||(s.charAt(i) == 'X' && (s.charAt(i + 1) == 'L' || s.charAt(i + 1) == 'C'))||(s.charAt(i) == 'C' && (s.charAt(i + 1) == 'D' || s.charAt(i + 1) == 'M'))) {
                result += (hashMap.get(s.charAt(i + 1)) - hashMap.get(s.charAt(i)));
                i++;
            }  else {
                result += hashMap.get(s.charAt(i));
            }
        }
        return result;
    }

    public int romanToInt2(String s) {
        HashMap<Character, Integer> hashMap = new HashMap<>();
        hashMap.put('I', 1);
        hashMap.put('V', 5);
        hashMap.put('X', 10);
        hashMap.put('L', 50);
        hashMap.put('C', 100);
        hashMap.put('D', 500);
        hashMap.put('M', 1000);
        int ans = 0;
        int n = s.length();
        for (int i = 0; i < n; ++i) {
            int value = hashMap.get(s.charAt(i));
            if (i < n - 1 && value < hashMap.get(s.charAt(i + 1))) {
                ans -= value;
            } else {
                ans += value;
            }
        }
        return ans;
    }

    @Test
    public void test() {
        int i = romanToInt("III");
        System.out.println(i);
    }

}
